package com.manage.competition.controller;

import com.manage.competition.common.ServerResponse;
import com.manage.competition.entity.User;
import com.manage.competition.service.IUserService;
import com.manage.competition.vo.UserVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Create with IDEA
 *
 * @Author:Vantcy
 * @Date: Create in 17:05 2019/1/23
 * @Description:
 */

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @RequiresRoles(value = {"admin","user","teacher"},logical = Logical.OR)
    @GetMapping("getUserInfo")
    public ServerResponse<UserVo> getUserInfo(){
        return iUserService.getUserInfo();
    }

    @RequiresRoles(value = {"admin"})
    @GetMapping("getUserList")
    public ServerResponse<List<UserVo>> getUserList(){
        return ServerResponse.createBySuccess(iUserService.getUserList());
    }

    @RequiresRoles(value = {"admin"})
    @GetMapping("getTeacherList")
    public ServerResponse<List<UserVo>> getTeacherList(){
        return ServerResponse.createBySuccess(iUserService.getTeacherList());
    }

//---------------------------------------------------------------------------------------

    @PostMapping("login")
    public ServerResponse<UserVo> login(String username, String password){
        return iUserService.login(username,password);
    }

    @PostMapping("logout")
    public ServerResponse<String> logout(){
        return iUserService.logout();
    }

    @PostMapping("register")
    public ServerResponse<String> register(String username,String password,String mail,String captcha){
        int status = iUserService.register(username,password,mail,captcha);
        if(status>0){
            return ServerResponse.createBySuccessMessage("用户成功注册");
        }else if (status==-1){
            return ServerResponse.createByErrorMessage("验证码过期");
        }else if (status==0){
            return ServerResponse.createByErrorMessage("验证码错误");
        }else if (status==-2){
            return ServerResponse.createByErrorMessage("用户名已经被使用");
        }
        return ServerResponse.createByErrorMessage("未知错误");
    }

    @PostMapping("registerValid")
    public ServerResponse<String> registerValid(String mail){
        if(iUserService.registerValid(mail)){
            return ServerResponse.createBySuccessMessage("邮件已发送");
        }
        return ServerResponse.createByErrorMessage("未知错误");
    }

    @PostMapping("userPrefileUpload")
    public ServerResponse<String> userPrefileUpload(@RequestParam(value = "prefile")MultipartFile prefile){
        String filename = iUserService.upload(prefile);
        if(filename!=null){
            return ServerResponse.createBySuccess(filename);
        }
        return ServerResponse.createByError();
    }

    @PostMapping("setUserInfo")
    public ServerResponse<String> setUserInfo(User user){
        Integer result = iUserService.setUserInfo(user);
        if(result!=null&&result>0){
            return ServerResponse.createBySuccess("success");
        }
        return ServerResponse.createByError();
    }

    @PostMapping("setNewPassword")
    public ServerResponse<String> setNewPassword(Long id,String newPassword){
        int result = iUserService.setNewPassword(id,newPassword);
        if(result>0){
            return ServerResponse.createBySuccess("success");
        }
        return ServerResponse.createByError();
    }

    @PostMapping("setNewRole")
    public ServerResponse<String> setNewRole(Long id,String newRole){
        int result = iUserService.setNewRole(id,newRole);
        if(result>0){
            return ServerResponse.createBySuccess("success");
        }
        return ServerResponse.createByError();
    }

    @PostMapping("setUserStatus")
    public ServerResponse<String> setUserStatus(Long id,int status){
        int result = iUserService.setUserStatus(id,status);
        if(result>0){
            return ServerResponse.createBySuccess("success");
        }
        return ServerResponse.createByError();
    }



}
